//
//  JRHttpRequest.h
//  
//
//  Created by dingjiarui on 2021/7/26.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/// 请求模式
typedef NSString * JRAFHTTPRequestMethod NS_TYPED_ENUM;

OBJC_EXTERN JRAFHTTPRequestMethod const JRAFHTTPRequestMethodGet;
OBJC_EXTERN JRAFHTTPRequestMethod const JRAFHTTPRequestMethodPost;
OBJC_EXTERN JRAFHTTPRequestMethod const JRAFHTTPRequestMethodPut;
OBJC_EXTERN JRAFHTTPRequestMethod const JRAFHTTPRequestMethodDelete;
/* 基础请求，不能用于上传、下载 */
OBJC_EXTERN JRAFHTTPRequestMethod const JRAFHTTPRequestMethodJsonGet;
OBJC_EXTERN JRAFHTTPRequestMethod const JRAFHTTPRequestMethodJsonPost;

/// 网络请求成功回调
typedef void(^JRHttpSuccessBlock)(NSURLResponse * _Nonnull response, id _Nullable responseObject);

/// 网络请求失败回调
typedef void(^JRHttpFailureBlock)(NSError * _Nullable error);

/// 网络请求流量回调
typedef void(^JRHttpTrafficBlock)(long long sendTraffic, long long receiveTraffic);

/// 网络请求进度回调
typedef void(^JRHttpProgressBlock)(long long totalBytes, long long totalBytesExpected);

/// 下载请求进度回调
typedef JRHttpProgressBlock JRHttpDownloadBlock;

/// 上传请求进度回调
typedef JRHttpProgressBlock JRHttpUploadBlock;

struct JRHttpRequest {
    /**
     请求方式
     */
    JRAFHTTPRequestMethod method;
    /**
     超时 >0
     */
    NSTimeInterval timeout;
    /**
     唯一key，判断是否同一个请求
     */
    NSString * _Nullable key;
    /**
     线程id，创建新线程；否则使用默认线程
     */
    NSString * _Nullable opId;
    /**
     线程优先级
     */
    NSOperationQueuePriority priority;
    /**
     请求头
     */
    NSDictionary<NSString *, NSString *> * _Nullable headerFields;
};

typedef struct JRHttpRequest JRHttpRequest;

FOUNDATION_EXPORT const JRHttpRequest JRHttpRequestGet;

static inline JRHttpRequest JRHttpRequestMake(JRAFHTTPRequestMethod method) {
    JRHttpRequest request = JRHttpRequestGet;
    request.method = method;
    return request;
}

static inline JRHttpRequest JRHttpOperationRequestMake(JRAFHTTPRequestMethod method, NSString * _Nullable key) {
    JRHttpRequest request = JRHttpRequestGet;
    request.method = method;
    request.key = key;
    return request;
}

static inline JRHttpRequest JRHttpOperationRequestMake2(JRAFHTTPRequestMethod method, NSString * _Nullable key, NSString * _Nullable opId)
{
    JRHttpRequest request = JRHttpRequestGet;
    request.method = method;
    request.key = key;
    request.opId = opId;
    return request;
}

@protocol JRHttpCallBackConvertible <NSObject>

@optional

@property (nonatomic, readonly, copy) id <JRHttpCallBackConvertible>
    (^onSuccess) (JRHttpSuccessBlock);
@property (nonatomic, readonly, copy) id <JRHttpCallBackConvertible>
    (^onFailure) (JRHttpFailureBlock);
@property (nonatomic, readonly, copy) id <JRHttpCallBackConvertible>
    (^onTraffic) (JRHttpTrafficBlock);
@property (nonatomic, readonly, copy) id <JRHttpCallBackConvertible>
    (^onDownload) (JRHttpProgressBlock);
@property (nonatomic, readonly, copy) id <JRHttpCallBackConvertible>
    (^onUpload) (JRHttpProgressBlock);

@end

@interface JRHttpCallBack : NSObject <JRHttpCallBackConvertible>

@property (nonatomic, copy, readonly) JRHttpSuccessBlock success;
@property (nonatomic, copy, readonly) JRHttpFailureBlock failure;
@property (nonatomic, copy, readonly) JRHttpTrafficBlock traffic;
@property (nonatomic, copy, readonly) JRHttpDownloadBlock download;
@property (nonatomic, copy, readonly) JRHttpUploadBlock upload;

@end

NS_ASSUME_NONNULL_END
